<?php

/**
 * uploadFile actions.
 *
 * @package    Amawal Invest
 * @subpackage uploadFile
 * @author     Ameen Shoman<amin.shoman@gmail.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class uploadFileActions extends sfActions
{
	/**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	public function executeIndex(sfWebRequest $request)
	{

	}
	public function executeUpload(sfWebRequest $request)
	{
		$the_file = $request->getFiles('the_file');
		$exp = explode('.',$the_file['name']) ;
		$ext = end($exp);

		$this->setTemplate('index');
		if(!in_array(strtolower($ext),globals::$FILES_TYPE_ALLOWED)){
			$this->getUser()->setFlashError('The file type uploaded is not allowed');
		}else{

			set_time_limit(0);
			TransactionPeer::UploadCsv($request->getFiles('the_file'));
			
			$this->getUser()->setFlashNotice('File uploaded successfully');
		}

	}
	public function executeRandGen(sfWebRequest $request)
	{
		$start_date = strtotime('2010-01-25');
		$end_date   = strtotime('2010-04-25');
		$funcs = array(
		'in-out'=>array('from'=>'01','to'=>'10','times'=>array('8','17'))//in-out
		, 'business'=>array('from'=>'02','to'=>'20','times'=>array('12','13'))//business
		, 'smoking'=>array('from'=>'03','to'=>'30','times'=>array('10','11'))//smoking
		, 'meeting'=>array('from'=>'04','to'=>'40','times'=>array('14','16'))//meeting
		);
		$leaves_types_arr = array( 'business' , 'smoking' , 'meeting' );
		$emps = array(
		'0001','0002','0003','0004','0005','0006','0007'
		);
		$no_of_vacation_prob_arr = array(
		0,0,0,0,0,0,0,1,2
		);
		$no_of_leaves_prob_arr = array(
		0,0,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,9,9,9
		);
		$prob_of_missing_tag = array(
		0,0,0,0,0,0,0,0,0,0,0,1
		);
		$times = range( 1 , 60 );

		$date_range = array();
		while ( $end_date >= $start_date ){
			$date_range[$start_date] = date('d/m/Y',$start_date);
			$start_date += (24*60*60);
		}

		$out = "";
		foreach ($date_range as $timestamp =>$date){
			$no_leaves = rand( 0 , 10 );
			shuffle( $no_of_vacation_prob_arr );
			shuffle( $no_of_leaves_prob_arr );
			$no_of_vacation = $no_of_vacation_prob_arr[0];
			$no_of_leaves = $no_of_leaves_prob_arr[0];

			shuffle($emps);
			$exsited_employees = array();
			for ( $i = 0 ; $i < count($emps) - $no_of_vacation ; $i++ ){
				$exsited_employees[$i] = $emps[$i];
			}

			for ( $i = 0 ; $i < count($exsited_employees) ; $i++ ){
				shuffle($times);

				$time_start = $funcs['in-out']['times'][0].':'.$times[0];
				shuffle($times);
				$time_end   = $funcs['in-out']['times'][1].':'.$times[1];

				$start_tag = $funcs['in-out']['from'];
				$end_tag   = $funcs['in-out']['to'];
				$lines[]   = "{$exsited_employees[$i]},$date,$time_start,$start_tag";
				$lines[]   = "{$exsited_employees[$i]},$date,$time_end,$end_tag";
			}

			for ( $i = 0 ; $i < $no_of_leaves ; $i++ ){
				shuffle($leaves_types_arr);
				$leave_type = $leaves_types_arr[0];
				shuffle($exsited_employees);
				$emp = $exsited_employees[0];

				$time_start = null;
				$time_end = null;

				shuffle($times);
				shuffle($prob_of_missing_tag);
				if(!$prob_of_missing_tag[0]){
					$time_start = $funcs[$leave_type]['times'][0].':'.$times[0];
				}
				shuffle($times);
				shuffle($prob_of_missing_tag);
				if(!$prob_of_missing_tag[0]){
					$time_end = $funcs[$leave_type]['times'][1].':'.$times[1];
				}

				$start_tag = $funcs[$leave_type]['from'];
				$end_tag   = $funcs[$leave_type]['to'];

				if($time_start){
					$lines[]   = "{$emp},$date,$time_start,$start_tag";
				}

				if($time_end){
					$lines[]   = "{$emp},$date,$time_end,$end_tag";
				}
			}

			//echo "<pre>";print_r($lines);exit;
			$out .= implode( "\n" , $lines )."\n";

		}///all loop

		file_put_contents( sfConfig::get('sf_cache_dir').'/'.date('y-m-d-h-i-s').'.csv' , $out );

		die('Done !');

		//echo "<pre>";print_r($date_range);exit;




	}
}
